package com.tedu.egcqweibo.controller;

import com.tedu.egcqweibo.mapper.UserMapper;
import com.tedu.egcqweibo.pojo.dto.UserRegDTO;
import com.tedu.egcqweibo.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation(value = "用户注册")
    @PostMapping("/reg")
    public int reg(@RequestBody UserRegDTO userRegDTO){
        log.trace("UserController.reg()开始被调用...");
        log.debug("UserRegDTO"+userRegDTO);
        log.info("开始处理用户注册");
        System.out.println("开始处理用户注册");
        if (userMapper.selectByUsername(userRegDTO.getUsername())!=null){
            return 2;
        }
        User user=new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        log.debug("User"+user);
        userMapper.insert(user);
        log.trace("UserController.reg()调用完毕");
        return 1;
    }
}
